home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: gail.ripco.com!mambuhl
- From: mambuhl@ripco.com (Martin Ambuhl)
- Subject: Re: Poor floating point c
- X-Nntp-Posting-Host: golden.ripco.com
- Message-ID: <DKJKA0.4wG@rci.ripco.com>
- Sender: usenet@rci.ripco.com (Net News Admin)
- Organization: Ripco Internet BBS Chicago
- Date: Tue, 2 Jan 1996 06:49:11 GMT
-
- >BC++ 4.51 seems to generate very poor floating point code. I would be
- >interested to know how well other compilers do on the following
- >examples. These were compiled using options: i486 CPU, fast floating
- >point, optimize for speed, large memory model.
-
- >main()
- > {
- > double x,y,z;
- > int n;
-
- > x = 4.5;
- > y = 10.7;
- > n = x;
- > z = x * y;
- > }
-
- [ No comments included about omitting return type or explicit return,
- this time. ]
- You're right. This is horrible optimization. gcc -O2 recognizes this
- code to do nothing, and optimizes it to
- main()
- {
- }
-
- If you make x, y, z, and n static, gcc -O2 produces:
-
- /* .file "tst.c" */
- /* gcc2_compiled.: */
- /* ___gnu_compiled_c: */
- /* .lcomm _x.2,8 */
- /* .lcomm _y.3,8 */
- /* .lcomm _z.4,8 */
- /* .lcomm _n.5,4 */
- /* .text */
- /* .align 2 */
- /* .globl _main */
- main()
- /* _main: */
- /* pushl %ebp */
- /* movl %esp,%ebp */
- /* subl $8,%esp */
- /* call ___main */
- {
- static double x,y,z;
- static int n;
- x = 4.5;
- /* movl $0,_x.2 */
- /* movl $1074921472,_x.2+4 */
- y = 10.7;
- /* movl $1717986918,_y.3 */
- /* movl $1076192870,_y.3+4 */
- n = x;
- /* movl $4,_n.5 */
- z = x * y;
- /* movl $858993459,_z.4 */
- /* movl $1078465331,_z.4+4 */
- }
- /* leave */
- /* ret */
-
-
-
-
- >Plain arithmetic, such as z = x * y, is generated inline as one would
- >expect. You would think the line n = x would be quite fast. However,
- >it generates a function call:
-
- >n=x;
- > wait
- > fld qword ptr[bp-0A]
- > call far FTOL@
- > mov [bp-02],ax
-
- Check the above. The compiler can _know_ the result of x * y and move
- it directly into z and can _know_ the value to put into n. There is no
- need to generate any arithmetic (inline or otherwise) or any
- conversions.
-
- [...]
-
- --
- * Martin Ambuhl net: mambuhl@ripco.com
- * Chicago, IL (USA)
-